package com.atmk.atmk.designMode.创建型.原型模式.demo3.深拷贝;

import lombok.Data;

import java.io.Serializable;
import java.io.*;
import java.util.List;
//使用序列化实现深度复制：
@Data
public class Person1 implements Cloneable, Serializable {
    private String name;
    private List<String> hobbies;

    @Override
    public Person1 clone() {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream out = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream in = null;
        try {
            bos = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bos);
            out.writeObject(this);

            bis = new ByteArrayInputStream(bos.toByteArray());
            in = new ObjectInputStream(bis);
            return (Person1) in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                bos.close();
                out.close();
                bis.close();
                in.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
